package com.yao.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.mall.annotation.AuthCheck;
import com.yao.mall.common.ErrorCode;
import com.yao.mall.common.Result;
import com.yao.mall.config.BusinessException;
import com.yao.mall.model.dto.shoppingOrder.GoodsSkuOrderRequest;
import com.yao.mall.model.dto.shoppingOrder.OrderCreateRequest;
import com.yao.mall.model.dto.shoppingOrder.OrderQueryRequest;
import com.yao.mall.model.entity.ShoppingOrder;
import com.yao.mall.model.vo.CreateOrderVO;
import com.yao.mall.model.vo.perOrder.PreOrderOfCartVO;
import com.yao.mall.model.vo.ShoppingOrderVO;
import com.yao.mall.model.vo.perOrder.PreOrderOfGoodsSkuVO;
import com.yao.mall.service.ShoppingOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author jiahua.yao
 * @version 1.0
 * @description 订单控制层
 * @date 2024/4/3 19:06
 */
@RestController
@RequestMapping("/shoppingOrder")
@Tag(name = "订单相关接口")
public class ShoppingOrderController {

    @Resource
    private ShoppingOrderService shoppingOrderService;

    @GetMapping("/createPreOrderOfCart")
    @Operation(summary = "预订单信息获取（购物车）")
    public Result<PreOrderOfCartVO> createPreOrderOfCart(){
        PreOrderOfCartVO preOrderVO = shoppingOrderService.preOrderOfCart();
        return Result.success(preOrderVO);
    }

    @PostMapping("/createPreOrderOfGoodsSku")
    @Operation(summary = "预订单信息获取（商品详情页）")
    public Result<PreOrderOfGoodsSkuVO> createPreOrderOfGoodsSku(@RequestBody GoodsSkuOrderRequest preOrderRequest){
        PreOrderOfGoodsSkuVO preOrderOfGoodsSku = shoppingOrderService.preOrderOfGoodsSku(preOrderRequest);
        return Result.success(preOrderOfGoodsSku);
    }

    @GetMapping("/orderDetail/{id}")
    @Operation(summary = "根据id获取订单详情信息")
    public Result<ShoppingOrderVO> orderDetail(@PathVariable Long id){
        ShoppingOrderVO shoppingOrderVO = shoppingOrderService.orderDetail(id);
        return Result.success(shoppingOrderVO);
    }


    @PostMapping("/createOrder")
    @Operation(summary = "创建订单")
    public Result<CreateOrderVO> createOrder(@RequestBody OrderCreateRequest orderCreateRequest){
        CreateOrderVO createOrderVO = shoppingOrderService.createOrder(orderCreateRequest);
        return Result.success(createOrderVO);
    }

    @GetMapping("/payDoneOrder/{orderId}")
    @Operation(summary = "获取当前已支付订单信息")
    public Result<ShoppingOrderVO> payDoneOrder(@PathVariable Long orderId){
        ShoppingOrder shoppingOrder = shoppingOrderService.getById(orderId);
        ShoppingOrderVO shoppingOrderVO = new ShoppingOrderVO();
        if(shoppingOrder.getOrderStatus() != 2){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"订单状态有误");
        }
        BeanUtils.copyProperties(shoppingOrder,shoppingOrderVO);
        return Result.success(shoppingOrderVO);
    }

    @PostMapping("/getOrderByStatus")
    @Operation(summary = "根据状态获取不同订单")
    public Result<Page<ShoppingOrderVO>> getOrderByStatus(@RequestBody OrderQueryRequest orderCreateRequest){
        Page<ShoppingOrderVO> shoppingOrderVO = shoppingOrderService.getOrderByStatus(orderCreateRequest);
        return Result.success(shoppingOrderVO);
    }

    @PostMapping("/toSend")
    @AuthCheck(mustRole = "1")
    @Operation(summary = "订单发货")
    public Result<Boolean> toSendGoods(@RequestParam("orderId") Long orderId){
        Boolean b = shoppingOrderService.toSendGoods(orderId);
        return Result.success(b);
    }
    @PostMapping("/toGetOrder")
    @Operation(summary = "订单收货")
    public Result<Boolean> toGetOrder(@RequestParam("orderId") Long orderId){
        Boolean b = shoppingOrderService.toGetOrder(orderId);
        return Result.success(b);
    }



}
